##IO distance need abut or at least 0.6um
##digital functional IO need to keep 10um to power/ground/corner/POC/clamp IO
##IVDDFS: core VDD IO
##IVSSFS: core VSS IO
##IVDDIOFS: IO power
##IVSSIOFS: IO ground
##IPOC: Power On Control cell,This library is designed to support power-on-control functions and each digital domain must include one 
##IFILLER0FS : Cell width is 0.1 um 
##IFILLER1FS : Cell width is 1 um
##IFILLER5FS : Cell width is 5 um
##IFILLER10FS : Cell width is 10 um
##ICORNERFS: corner cell for digital core
#
set height 90.5
set llx [lindex [dbget top.fplan.box] 0 0]
set lly [lindex [dbget top.fplan.box] 0 1]
set urx [lindex [dbget top.fplan.box] 0 2]
set ury [lindex [dbget top.fplan.box] 0 3]



set height_c [expr $die_height - 2*$height]
set width_c [expr $die_width - 2*$height - 2*570]

set left_s 200     
#4
set bottom_s 90
#13
set right_s 200 
#4
set top_s 95
#12

set left_e [expr $left_s - 20]
set bottom_e [expr $bottom_s - 10]
set right_e [expr $right_s - 20]
set top_e [expr $top_s - 30]

set left_o [expr $left_e/2]
set bottom_o [expr $bottom_e/2]
set right_o [expr $right_e/2]
#set top_o [expr $top_e/2]
set top_o 400

addInst -physical -status fixed -cell ICORNER -inst corner_left_bottom -loc {0 0} -ori R0
addInst -physical -status fixed -cell ICORNER -inst corner_left_top -loc "$llx [expr $ury - $height]" -ori MX
addInst -physical -status fixed -cell ICORNER -inst corner_right_bottom -loc "[expr $urx - $height] $lly" -ori R90
addInst -physical -status fixed -cell ICORNER -inst corner_rigth_top -loc "[expr $urx - $height] [expr $ury -$height]" -ori R180



# left
addInst -physical -status fixed -cell IDCLAMPC -inst clamp_0 -loc "$llx [expr $lly + 90.5 + 0*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVDD -inst VDDC_0 -loc "$llx [expr $lly + 90.5 + 1*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVSS -inst VSSC_0 -loc "$llx [expr $lly + 90.5 + 2*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVDDIO -inst VDDIO_0 -loc "$llx [expr $lly + 90.5 + 3*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVSSIO -inst VSSIO_0 -loc "$llx [expr $lly + 90.5 + 4*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVDD -inst VDDC_1 -loc "$llx [expr $lly + 90.5 + 5*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IVSS -inst VSSC_1 -loc "$llx [expr $lly + 90.5 + 6*$left_s + $left_o]" -ori R270
addInst -physical -status fixed -cell IDCLAMPC -inst clamp_1 -loc "$llx [expr $lly + 90.5 + 7*$left_s + $left_o]" -ori R270


# bottom
addInst -physical -cell IVDDIO -inst VDDIO_2 
addInst -physical -cell IVSSIO -inst VSSIO_2 
addInst -physical -cell IDCLAMPC -inst clamp_4 
addInst -physical -cell IVDD -inst VDDC_4 
addInst -physical -cell IVSS -inst VSSC_4 

placeInstance gpio_0__inout_gpio_i [expr $lly + 90.5 + 0*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_1__inout_gpio_i [expr $lly + 90.5 + 1*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_2__inout_gpio_i [expr $lly + 90.5 + 2*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_3__inout_gpio_i [expr $lly + 90.5 + 3*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_4__inout_gpio_i [expr $lly + 90.5 + 4*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_5__inout_gpio_i [expr $lly + 90.5 + 5*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance VDDIO_2  [expr $lly + 90.5 + 6*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance VSSIO_2  [expr $lly + 90.5 + 7*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance clamp_4  [expr $lly + 90.5 + 8*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance VDDC_4  [expr $lly + 90.5 + 9*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance VSSC_4  [expr $lly + 90.5 + 10*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_6__inout_gpio_i [expr $lly + 90.5 + 11*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_7__inout_gpio_i [expr $lly + 90.5 + 12*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_8__inout_gpio_i [expr $lly + 90.5 + 13*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_9__inout_gpio_i [expr $lly + 90.5 + 14*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_10__inout_gpio_i [expr $lly + 90.5 + 15*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_11__inout_gpio_i [expr $lly + 90.5 + 16*$bottom_s + $bottom_o]  $lly -fixed R0
placeInstance gpio_12__inout_gpio_i [expr $lly + 90.5 + 17*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_13__inout_gpio_i [expr $lly + 90.5 + 18*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_14__inout_gpio_i [expr $lly + 90.5 + 19*$bottom_s + $bottom_o] $lly -fixed R0
placeInstance gpio_15__inout_gpio_i [expr $lly + 90.5 + 20*$bottom_s + $bottom_o] $lly -fixed R0



# right
addInst -physical -status fixed -cell IDCLAMPC -inst clamp_2 -loc "[expr $urx - $height] [expr $lly + 90.5 + 0*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVDD -inst VDDC_2 -loc "[expr $urx - $height] [expr $lly + 90.5 + 1*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVSS -inst VSSC_2 -loc "[expr $urx - $height] [expr $lly + 90.5 + 2*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVDDIO -inst VDDIO_1 -loc "[expr $urx - $height] [expr $lly + 90.5 + 3*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVSSIO -inst VSSIO_1 -loc "[expr $urx - $height] [expr $lly + 90.5 + 4*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVDD -inst VDDC_3 -loc "[expr $urx - $height] [expr $lly + 90.5 + 5*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IVSS -inst VSSC_3 -loc "[expr $urx - $height] [expr $lly + 90.5 + 6*$right_s + $right_o]" -ori R90
addInst -physical -status fixed -cell IDCLAMPC -inst clamp_3 -loc "[expr $urx - $height] [expr $lly + 90.5 + 7*$right_s + $right_o]" -ori R90


# top
addInst -physical -cell IVDDIO -inst VDDIO_3 
addInst -physical -cell IVSSIO -inst VSSIO_3 
addInst -physical -cell IDCLAMPC -inst clamp_5 
addInst -physical -cell IVDD -inst VDDC_5 
addInst -physical -cell IVSS -inst VSSC_5

placeInstance input_spi_miso "[expr $llx +  90.5 + 0*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_rx_pin "[expr $llx + 90.5 + 1*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_rstn "[expr $llx + 90.5 + 2*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance VDDIO_3 "[expr $llx + 90.5 + 3*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance VSSIO_3 "[expr $llx + 90.5 + 4*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance clamp_5 "[expr $llx + 90.5 + 5*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance VDDC_5 "[expr $llx + 90.5 + 6*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance VSSC_5 "[expr $llx + 90.5 + 7*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance input_clk "[expr $llx + 90.5 + 8*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_spi_clk "[expr $llx + 90.5 + 9*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_spi_ss "[expr $llx + 90.5 + 10*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_spi_mosi "[expr $llx + 90.5 + 11*$top_s + $top_o] [expr $ury - $height]" -fixed R180
placeInstance output_tx_pin "[expr $llx + 90.5 + 12*$top_s + $top_o] [expr $ury - $height]" -fixed R180




####foreach inst [dbget -v [dbget -p2 top.insts.cell.baseClass pad].name corner*] {
####		set box [dbget [dbget -p top.insts.name $inst].box]
####		set orient [dbget [dbget -p top.insts.name $inst].orient]
####		switch $orient {
####				R0 {
####						set lx [expr [lindex $box 0 0] + 5]
####						set ux [expr [lindex $box 0 2] - 5]
####						set ly [expr [lindex $box 0 1] + 7]
####						set uy [expr [lindex $box 0 3] - 20.5]
####						add_gui_shape -layer bond90_0 -rect "$lx $ly $ux $uy"
####						add_gui_shape -layer bond90_9 -rect "$lx $ly $ux $uy"
####				}
####				R270 {
####						set lx [expr [lindex $box 0 0] + 7]
####						set ux [expr [lindex $box 0 2] - 20.5]
####						set ly [expr [lindex $box 0 1] + 5]
####						set uy [expr [lindex $box 0 3] - 5]
####						add_gui_shape -layer bond90_0 -rect "$lx $ly $ux $uy"
####						add_gui_shape -layer bond90_9 -rect "$lx $ly $ux $uy"
####				}
####				R180 {
####						set lx [expr [lindex $box 0 0] + 5]
####						set ux [expr [lindex $box 0 2] - 5]
####						set ly [expr [lindex $box 0 1] + 20.5]
####						set uy [expr [lindex $box 0 3] - 7]
####						add_gui_shape -layer bond90_0 -rect "$lx $ly $ux $uy"
####						add_gui_shape -layer bond90_9 -rect "$lx $ly $ux $uy"
####				}
####				R90 {
####						set lx [expr [lindex $box 0 0] + 20.5]
####						set ux [expr [lindex $box 0 2] - 7]
####						set ly [expr [lindex $box 0 1] + 5]
####						set uy [expr [lindex $box 0 3] - 5]
####						add_gui_shape -layer bond90_0 -rect "$lx $ly $ux $uy"
####						add_gui_shape -layer bond90_9 -rect "$lx $ly $ux $uy"
####				}				
####		}
####}


#deleteIoFiller
addIoFiller -cell IFILLER10 -prefix IO_FILLER 
addIoFiller -cell IFILLER5 -prefix IO_FILLER 
addIoFiller -cell IFILLER1 -prefix IO_FILLER 
addIoFiller -cell IFILLER0 -prefix IO_FILLER -fillAnyGap
fixAllIos
#attachIOBuffer

# add ESD core diode
#addInst -cell DI_GIO -inst ESD_diode_ground2signal -physical -ori MX90 -loc "1000 1238"
#addInst -cell DI_PIO -inst ESD_diode_power2signal -physical -ori MX90 -loc "1060 1238"
# add ESD core clamp
addInst -cell PC_C -inst ESD_clamp_core_power2ground -physical -status fixed -ori R0 -loc "1000 126"
addHaloToBlock {5 5 5 5} -snapToSite ESD_clamp_core_power2ground



